import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:moose_app/core/utils/navigate_util.dart';
import 'package:moose_app/ui/screens/search/search_screen.dart';
import 'package:moose_app/ui/values/values.dart';
import 'package:moose_app/ui/widgets/icon_back_button.dart';
import 'package:moose_app/ui/widgets/scroll/scroller_configuration_wrapper.dart';
import 'package:moose_app/ui/widgets/search/search_input_view.dart';
import 'package:moose_app/ui/widgets/common/common_more_sheet.dart';

import 'follow/follow_content.dart';
import 'home_sticky_tabbar_delegate.dart';
import 'ranking/ranking_content.dart';
import 'recommend/recommend_content.dart';

class ATHHomeBody extends StatefulWidget {
  @override
  _ATHHomeBodyState createState() => _ATHHomeBodyState();
}

class _ATHHomeBodyState extends State<ATHHomeBody>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  TabController _tabController;

  List<String> _tabsText = ["关注", "推荐", "排行榜", "标签", "成语", "阅读"];

  List<Tab> _tabs = [];

  @override
  void initState() {
    super.initState();
    _tabsText.forEach((element) {
      _tabs.add(Tab(
        text: element,
      ));
    });

    _tabController =
        TabController(initialIndex: 1, length: _tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return ATHScrollerConfigurationWrapper(
      child: CustomScrollView(
        slivers: <Widget>[
          _buildAppBar(),
          SliverPersistentHeader(
            pinned: true,
            delegate: ATHHomeStickyTabBarDelegate(
                child: _buildTabBar(),
                press: () {
                  print("More Icon Press");
                  _showMoreSheet();
                }),
          ),
          SliverFillRemaining(
            child: _buildTabBarView(),
          ),
        ],
      ),
    );
  }

  _showMoreSheet() async {
    await showCupertinoModalBottomSheet(
      expand: false,
      context: context,
      backgroundColor: Colors.white,
      builder: (BuildContext context) {
        return ATHCommonMoreSheet();
      },
    );
  }

  TabBar _buildTabBar() {
    return TabBar(
      isScrollable: true,
      controller: _tabController,
      tabs: _tabs,
      indicatorColor: kColor33,
      indicatorSize: TabBarIndicatorSize.label,
      labelStyle: TextStyle(fontSize: kFontSize32, fontWeight: FontWeight.bold),
      labelColor: kColor33,
      unselectedLabelStyle: TextStyle(fontSize: kFontSize30),
      unselectedLabelColor: kColor99,
    );
  }

  Widget _buildAppBar() {
    return SliverAppBar(
      pinned: true,
      elevation: 0,
      title: ATHSearchInputView(
        press: () {
          ATHNavigator.pushFromRight(context, ATHSearchScreen.routeName);
        },
      ),
      leading: ATHIconBackButton(
        iconPath: "assets/icons/icon_location.svg",
        press: () {
          print("icon_location");
        },
      ),
    );
  }

  TabBarView _buildTabBarView() {
    List<Widget> tabContents = [];
    for (int index = 0; index < _tabsText.length; index++) {
      if (index == 0) {
        tabContents.add(ATHFollowContent());
      } else if (index == 1) {
        tabContents.add(ATHRecommendContent());
      } else if (index == 2) {
        tabContents.add(ATHRankingContent());
      } else {
        tabContents.add(Center(child: Text(_tabsText[index])));
      }
    }
    return TabBarView(controller: _tabController, children: tabContents);
  }

  @override
  bool get wantKeepAlive => true;
}
